package com.loong.system.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loong.framework.dto.IdParamDTO;
import com.loong.framework.utils.SystemUtil;
import com.loong.system.dto.*;
import com.loong.system.entity.Tenant;
import com.loong.system.service.IDictService;
import com.loong.system.service.ITenantService;
import com.loong.system.vo.TenantDetailVO;
import com.loong.system.vo.TenantListVO;
import com.loong.system.vo.TenantShowListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "租户操作 API")
@RestController
@RequestMapping("/api/v1.0/tenant")
public class TenantController {

  @Resource private ITenantService tenantService;
  @Resource private IDictService dictService;

  @SaCheckRole("root")
  @Operation(summary = "新增")
  @PostMapping("/add")
  public void add(@RequestBody @Valid TenantAddDTO addDTO) {
    Tenant tenant = SystemUtil.buildResource(addDTO, Tenant.class);
    tenantService.addTenant(tenant);
  }

  @SaCheckRole("root")
  @Operation(summary = "删除")
  @PostMapping("/remove")
  public void remove(@RequestBody @Valid IdParamDTO idParamDTO) {
    tenantService.removeById(idParamDTO.getId());
  }

  @SaCheckRole("root")
  @Operation(summary = "更新")
  @PostMapping("/update")
  public void update(@RequestBody @Valid TenantUpdateDTO updateDTO) {
    Tenant tenant = SystemUtil.buildResource(updateDTO, Tenant.class);
    tenantService.updateById(tenant);
  }

  @Operation(summary = "更新自身")
  @PostMapping("/updateStoreInfo")
  public void updateStoreInfo(@RequestBody @Valid TenantInfoUpdateDTO updateDTO) {
    Tenant tenant = SystemUtil.buildResource(updateDTO, Tenant.class);
    tenantService.updateById(tenant);
  }

  @SaCheckRole("root")
  @Operation(summary = "详情")
  @PostMapping("/detail")
  public TenantDetailVO detail(@RequestBody @Valid IdParamDTO idParamDTO) {
    Tenant tenant = tenantService.getById(idParamDTO.getId());
    TenantDetailVO buildResource = SystemUtil.buildResource(tenant, TenantDetailVO.class);
    dictService.transfer(buildResource);
    return buildResource;
  }

  @Operation(summary = "详情")
  @PostMapping("/tenantInfoDetail")
  public TenantShowListVO tenantInfoDetail(@RequestBody @Valid IdParamDTO idParamDTO) {
    Tenant tenant = tenantService.getById(idParamDTO.getId());
    return SystemUtil.buildResource(tenant, TenantShowListVO.class);
  }

  @SaCheckRole("root")
  @Operation(summary = "分页获取租户列表")
  @PostMapping("/pageTenantList")
  public Page<TenantListVO> pageTenantList(@RequestBody @Valid TenantPageDTO pageDTO) {
    Page<Tenant> page = tenantService.pageTenantList(pageDTO);
    Page<TenantListVO> builtResource = SystemUtil.buildResource(page, TenantListVO.class);
    dictService.transfer(builtResource);
    return builtResource;
  }

  @Operation(summary = "获取租户自身信息列表（本来应该是个detail，但是为了前端框架方便）")
  @PostMapping("/pageTenantInfoList")
  public Page<TenantShowListVO> pageTenantInfoList(@RequestBody @Valid TenantPageDTO pageDTO) {
    Object tenantId = StpUtil.getSession().get("tenantId");
    pageDTO.setTenantId(tenantId.toString());
    Page<Tenant> page = tenantService.pageTenantList(pageDTO);
    return SystemUtil.buildResource(page, TenantShowListVO.class);
  }

  @SaIgnore
  @Operation(summary = "获取租户自身信息列表（app）")
  @PostMapping("/pageTenantInfoList2App")
  public Page<TenantShowListVO> pageTenantInfoList2App(
      @RequestBody @Valid TenantInfoPageDTO pageDTO) {
      return tenantService.pageTenantShowList(pageDTO);
  }
}
